const Koa = require("koa");
const app = new Koa();

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set("X-Response-Time", `${ms}ms`);
  ctx.set("X-crftoken", "abcdefg");
});

app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.get("X-Response-Time");
  const crfToken = ctx.response.get("X-crftoken");
  console.log(`${ctx.method} ${ctx.url} - ${rt}`);
  console.log(`${ctx.method} ${ctx.url} - token - ${crfToken}`);
});

app.use(async (ctx) => {
  ctx.body = "Hello world!";
});

app.listen(3000, () => {
  console.log("at port 3000");
});
